﻿namespace WMS6.Web.Administration
{
    using System;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Data;
    using System.IO;
    using System.Net;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;

    public class ScriptCreated : BasePage
    {
        protected Button btnCreated;
        protected Button btnGoBack;
        protected HtmlForm F2122;
        protected GridView gvScriptCreated;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlHead Head1;
        protected Label lblMsg;
        protected Label lbScriptCreated;
        protected HtmlLink mycss;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2AuditDataList()
        {
            DataSet ds = new DataSet();
            ds = RoleFunctionServiceClientProxy.GetInstance().GetAllFunctionList();
            this.gvScriptCreated.DataSource = ds.Tables[0];
            this.gvScriptCreated.DataBind();
        }

        protected void btnCreated_Click(object sender, EventArgs e)
        {
            List<string> URL = base.GetSelectedIDsInGridView(this.gvScriptCreated, "URL");
            if (URL.Count == 0)
            {
                this.lblMsg.Text = "请选择一项！";
            }
            else
            {
                for (int i = 0; URL.Count > i; i++)
                {
                    this.SetSessionGetAimUrl(URL[i]);
                }
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            base.Response.Redirect("GlobalSetting.aspx", false);
        }

        protected void gvScriptCreated_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            NameValueCollection nvc = base.Request.ServerVariables;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                string URI = "http://" + nvc.Get("SERVER_NAME") + ":" + nvc.Get("SERVER_PORT") + "/PowerWMS6Web" + e.Row.Cells[2].Text.Replace("~", "") + "?ScriptCreated=Created";
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "CreatedScript(this,'" + URI + "');");
            }
        }

        private void LoadData()
        {
            this.BindData2AuditDataList();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.LoadData();
            }
        }

        protected string SetSessionGetAimUrl(string AimUrl)
        {
            try
            {
                CookieContainer cookieContainer = new CookieContainer();
                if (this.ViewState["cookieContainer"] != null)
                {
                    cookieContainer = (CookieContainer) this.ViewState["cookieContainer"];
                }
                NameValueCollection nvc = base.Request.ServerVariables;
                HttpWebRequest request = WebRequest.Create("http://" + nvc.Get("SERVER_NAME") + ":" + nvc.Get("SERVER_PORT") + "/PowerWMS6Web" + AimUrl.Replace("~", "")) as HttpWebRequest;
                request.Method = "";
                request.KeepAlive = false;
                request.CookieContainer = cookieContainer;
                Stream responseStream = (request.GetResponse() as HttpWebResponse).GetResponseStream();
                StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                string srcString = reader.ReadToEnd();
                reader.Close();
                responseStream.Close();
                return srcString;
            }
            catch
            {
                return "";
            }
        }
    }
}

